﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.Common;
using System.Globalization;
using HRProject.Code.DTO;
using HRProject.Code.DAO;
using System.Collections.Generic;

namespace HRProject.Asset
{
    public partial class AjaxAsset2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int errorCode = 0;
            int updateResult = 0;
            string error = "";
            string result = "";

            string sOperation = Request.Params["operation"];
            string sAssetTypeId = Request.Params["assetTypeId"];
		    string sAssetId = Request.Params["assetId"];
			string sAssetName = Request.Params["assetName"];
			string sDescription = Request.Params["description"];
			string sStartDate = Request.Params["startDate"];
            string sEndDate = Request.Params["endDate"];

            bool newAsset = false;
            Guid assetId;
            Guid assetTypeId;
            try
            {
                assetTypeId = new Guid(sAssetTypeId);
            }
            catch (Exception exp)
            {
                assetTypeId = Guid.Empty;
                Console.Out.WriteLine(exp.Message);
            }

            
            try
            {
                assetId = new Guid(sAssetId);
            }
            catch (Exception exp)
            {
                assetId = Guid.NewGuid();
                newAsset = true;
                Console.Out.WriteLine(exp.Message);
            }

            if ("select_node".Equals(sOperation))
            {
                AssetDAO assetDao = new AssetDAO();
                BLAsset filter = new BLAsset();
                filter.Id = Guid.Empty;
                filter.Name = string.Empty;
                filter.Type = new BLAssetType();
                filter.Type.Id = assetTypeId;
                filter.ImportDate = Constant.DefaultMinDate;
                filter.TerminateDate = Constant.DefaultMaxDate;
                List<BLAsset> assets = assetDao.Search(filter, "AssetName", true, 1, 200, 1);

                result = "[";
                string format = new string('0', Convert.ToString(assets.Count).Length);
                for (int i = 0; i < assets.Count; i++)
                {
                    if (i > 0)
                    {
                        result += ",";
                    }
                    result += "{";
                    result += "\"order\":\"" + (i+1).ToString(format) + "\"";
                    result += ",\"assetName\":\"" + assets[i].Name + "\"";
                    result += ",\"assetId\":\"" + assets[i].Id + "\"";
                    result += ",\"description\":\"" + assets[i].Description + "\"";
                    result += ",\"startDate\":\"" + assets[i].ImportDate.ToString(Constant.DateFormat) + "\"";
                    result += ",\"endDate\":\"" + assets[i].TerminateDate.ToString(Constant.DateFormat) + "\"";
                    result += "}";
                }
                result += "]";
                result = "{\"assets\":" + result + "}";

                Response.Clear();
                Response.ContentType = "application/json; charset=utf-8";
                Response.Write(result);
                Response.End();
                return;
            }

            if ("remove_asset".Equals(sOperation))
            {
                AssetDAO assetDao = new AssetDAO();
                BLAsset filter = new BLAsset();
                filter.Id = assetId;
                updateResult = assetDao.Delete(filter, ref errorCode);

                error = "[";
                if (errorCode == 12)
                {
                    error += "\"" + ContentProvider.GetMessage("NotAuthorizedDetail").Replace("{0}", "xóa thông tin tài sản") + "\"";
                }
                error += "]";
            }
            else
            {
                DateTime startDate;
                DateTime endDate;

                error = "[";
                if (sStartDate == null || sEndDate == null
                    || !DateTime.TryParseExact(sStartDate, Constant.DateTimeFormats, Constant.enCulture, DateTimeStyles.None, out startDate)
                    || !DateTime.TryParseExact(sEndDate, Constant.DateTimeFormats, Constant.enCulture, DateTimeStyles.None, out endDate))
                {
                    error += "\"" + ContentProvider.GetMessage("InvalidDateFormat") + "\"";
                }
                else
                {
                    AssetDAO assetDao = new AssetDAO();
                    BLAsset asset = new BLAsset();
                    asset.Id = assetId;
                    asset.Name = sAssetName;
                    asset.Type.Id = assetTypeId;
                    asset.Description = sDescription;
                    asset.ImportDate = startDate;
                    asset.TerminateDate = endDate;
                    if (newAsset)
                    {
                        updateResult = assetDao.QuickAdd(asset, ref errorCode);
                    }
                    else
                    {
                        updateResult = assetDao.QuickUpdate(asset, ref errorCode);
                    }
                }

                if (errorCode == 12)
                {
                    error += "\"" + ContentProvider.GetMessage("NotAuthorizedDetail").Replace("{0}", "cập nhật thông tin tài sản") + "\"";
                }
                error += "]";
            }

            result = "{\"status\":" + (errorCode==0?"true":"false")
                + ",\"assetId\":\"" + assetId 
                + "\",\"error\": "+ error + "}";
            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Write(result);
            Response.End();
        }
    }
}
